#ifndef I2C_H
#define I2C_H

#include "IO.h"

#include <unistd.h> //for usleep
/*Description

I2C wrapper class.  Derived class of IO defined in IO.h (uses clr, set functions)

*/

//#define SDA P1_7
//#define SCL P1_6
/*
#define "SDA = 1;" "this.set(SDAbit);"
#define "SDA = 0;" this.clr(SDAbit);
#define "SCL = 1;" this.set(SCLbit);
#define "SCL = 0;" this.clr(SCLbit);

#define       "ackbit = SDA;"  ackbit=this.getbit(SDAbit);           
#define        "while(!SCL);"  while(!this.getbit(SCLbit);
#define "if (SDA) byte |= i;"  if(this.getbit(SDAbit))byte|=i;
*/
typedef volatile unsigned char tvPTR;

class I2C : IO
{
        public:
                I2C(tvPTR *DATA, tvPTR *DIR, char pdatabit, char pclockbit);
                ~I2C();
                
                unsigned char i2c_read(unsigned char slave_addr);
                void i2c_write(unsigned char slave_addr, unsigned char byte); //writes byte to given slave addr
                     
        private:
                tvPTR *PORT;
                //bool SDA;
               // bool SCL;
                
                char SDAbit;
                char SCLbit;
                                         
                unsigned char i2c_tx(unsigned char byte);
                unsigned char i2c_rx(unsigned char ack);
                
                void i2c_start();
                void i2c_stop();
};

#endif
